package com.example.leetcode.nknc78;

import com.example.leetcode.ListNode;

import java.util.Stack;

public class Solution {

    public ListNode ReverseList(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }
        ListNode ans = new ListNode(0);
        System.out.println(stack.size());
        while (!stack.isEmpty()) {
            ans.next = new ListNode(stack.pop());
        }
        return ans.next;
    }

    public ListNode ReverseList2(ListNode head) {
        Stack<Integer> stack = new Stack<>();
         while (head != null) {
             stack.push(head.val);
             head = head.next;
         }
         ListNode ans = new ListNode(0);
         ListNode temp = ans;
         System.out.println(stack.size());
         while (!stack.isEmpty()) {
             temp.next = new ListNode(stack.pop());
             temp = temp.next;
         }
         return ans.next;
    }

    public ListNode ReverseList3(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode reverse = ReverseList3(head.next);
        head.next.next = head;
        head.next = null;
        return reverse;
    }
}
